﻿/*
 * 名称：程序设计语言-编译原理算法，语法分析的父类
 * 制作者：WT
 * 创建时间：2012-10-23
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CompilerDLL.SyntaxAnalysis
{
    public class BaseSyntaxParse
    {

        #region 属性定义
        protected  Stack stackProcSymbols = new Stack();

        /// <summary>
        /// 栈，存放产生式产生式中的终结符号或者非终结符号
        /// </summary>
        public Stack StackProcSymbols
        {
            get { return stackProcSymbols; }
            set { stackProcSymbols = value; }
        }

        protected ArrayList arrayTokens = new ArrayList();

        /// <summary>
        /// 所要进行语法分析的单词符号输入串，元素类型为ModelTokenType
        /// </summary>
        public ArrayList ArrayTokens
        {
            get { return arrayTokens; }
            set { arrayTokens = value; }
        }

        protected ArrayList arraySyntaxAnalysisSteps = new ArrayList();

        /// <summary>
        /// 记录预测分析计算过程步骤，其中的元素类型为Model_LL1_StepStatus
        /// </summary>
        public ArrayList ArraySyntaxAnalysisSteps
        {
            get { return arraySyntaxAnalysisSteps; }
            set { arraySyntaxAnalysisSteps = value; }
        }


        protected Hashtable hashForecastTable = new Hashtable();

        /// <summary>
        /// Hashtable,按照M[A,a]的结构，存储LL（1）的分析表，访问其产生式
        /// 其中A为非终结符
        /// a为终结符
        /// Hashtable中存储的元素的类型为ModelProduction
        /// </summary>
        public Hashtable HashForecastTable
        {
            get { return hashForecastTable; }
            set { hashForecastTable = value; }
        }

        protected Hashtable hashSymbolType = new Hashtable();

        /// <summary>
        /// 定义产生式中的字符是否为终结符，如果是则为1，否则为0；
        /// </summary>
        public Hashtable HashSymbolType
        {
            get { return hashSymbolType; }
            set { hashSymbolType = value; }
        }

        #region 属性
        protected Hashtable hashGrammarReverseProduction = new Hashtable();

        /// <summary>
        /// 按逆向产生式保存的文法结构，即按产生式的右部进行索引，
        /// 如E->TE，则索引时按Hashtable[TE]=E进行索引
        /// </summary>
        public Hashtable HashGrammarReverseProduction
        {
            get { return hashGrammarReverseProduction; }
            set { hashGrammarReverseProduction = value; }
        }

        #endregion 

        //构造语法分析树所依赖的栈，用于跟踪构造线索  
        protected Stack stackSyntaxTreeNode = new Stack();

        /// <summary>
        /// 构造语法分析树所依赖的栈，用于跟踪构造线索  
        /// </summary>
        public Stack StackSyntaxTreeNode
        {
            get { return stackSyntaxTreeNode; }
            set { stackSyntaxTreeNode = value; }
        }

        protected SyntaxTreeNode syntaxTree;

        /// <summary>
        /// 刚执行完语法分析时，语法分析所得到的语法树的即时状态
        /// </summary>
        public SyntaxTreeNode SyntaxTree
        {
            get { return syntaxTree; }
            set { syntaxTree = value; }
        }


        #endregion

        #region 可被子类中重写的函数
        /// <summary>
        ///  语法分析主程序，可被子类中的函数重写
        /// </summary>
        /// <param name="arrayTokens"></param>
        /// <param name="strPara"></param>
        /// <returns></returns>
        public virtual ArrayList Syntax_Parse_Main(
            ArrayList arrayTokens,
            string strPara)
        {
            return null;
        }
        #endregion 

        #region 可被子类继承的函数
        #region 初始化算法条件，改造输入串
        /// <summary>
        /// 改造输入串，在末尾加入“#”。
        /// </summary>
        /// <param name="arrayTokens"></param>
        protected void ModifyTheInputTokens(ArrayList arrayTokens)
        {
            //在输入串中加入一个“#”，以构造终止条件
            LexicalAnalysis.ModelTokenType m =
                new CompilerDLL.LexicalAnalysis.ModelTokenType();
            m.Lexem = "#";
            arrayTokens.Add(m);
            this.arrayTokens = arrayTokens;
        }
          
        #endregion
        #endregion 
    }
}
